package com.yaozheng.vocationaltraining.activity;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.IHomeAnswerTaskView;
import com.yaozheng.vocationaltraining.service.HomeAnswerTaskService;
import com.yaozheng.vocationaltraining.service.impl.HomeAnswerTaskServiceImpl;
import com.yaozheng.vocationaltraining.utils.QuestionUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.HomeAnswerTaskView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 每日一测
 */
@EActivity(R.layout.activity_home_answer_task)
public class HomeAnswerTaskActivity extends BaseActivity implements IHomeAnswerTaskView {
    @Bean(HomeAnswerTaskServiceImpl.class)
    HomeAnswerTaskService homeAnswerTaskService;
    @ViewById
    HomeAnswerTaskView homeAnswerTaskView;


    JSONObject paperJsonObject;


    @AfterViews
    public void initView() {
        setHeadTitle("每日一测");
        homeAnswerTaskService.init(this);
        showOperatingProgressDialog("获取题目中...");
        homeAnswerTaskService.getQuestionList();


    }

    @UiThread
    @Override
    public void getQuestionListSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        paperJsonObject = TypeUtils.getJsonObject(jsonObject,
                "data", "paper");
        homeAnswerTaskView.loadData(paperJsonObject);
    }

    @UiThread
    @Override
    public void getQuestionListError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @UiThread
    @Override
    public void submitAnswerSuccesss(JSONObject jsonObject) {
        cancelOperatingProgressDialog();

        JSONObject resultJsonObject = TypeUtils.getJsonObject(jsonObject,
                "result");
        int correct = TypeUtils.getJsonInteger(resultJsonObject, "correct", 0);
        int incorrect = TypeUtils.getJsonInteger(resultJsonObject, "incorrect",
                0);
        int award = TypeUtils.getJsonInteger(resultJsonObject, "award", 0);
//        alertMessage("答对" + correct + "题,答错" + incorrect + "题,奖励" + award + "分");
        StringBuffer carryOutAnAssignmentAskValue = new StringBuffer();
        carryOutAnAssignmentAskValue.append("答对" + correct + "题");
        carryOutAnAssignmentAskValue.append("\n");
        carryOutAnAssignmentAskValue.append("答错" + incorrect + "题");
        carryOutAnAssignmentAskValue.append("\n");
        carryOutAnAssignmentAskValue.append("获得积分" + award);
        showCancelAskDialog(carryOutAnAssignmentAskValue.toString(), "每日一测", "onBackPressed");
    }

    @UiThread
    @Override
    public void submitAnswerError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
        onBackPressed();
    }


    @Override
    public boolean isResponseAnswerResult() {

        return isResponseResult();
    }

    @Override
    public void responseSuccess(JSONObject jsonObject) {

    }

    @Override
    public void responseError(String error) {

    }

    @Click(R.id.closeImageView)
    public void backClick() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Click(R.id.submitImageView)
    public void submitClick() {
        if (paperJsonObject != null) {
            JSONArray questionArray = TypeUtils.getJsonArray(paperJsonObject,
                    "questions");
            String answers = QuestionUtils.getAnswers(questionArray);
            if (answers != null) {
                if (homeAnswerTaskService != null) {
                    homeAnswerTaskService.submitAnswer(
                            TypeUtils.getJsonString(paperJsonObject, "id"),
                            answers);
                    return;
                }
            } else {
                //答案为null
                alertMessage("答案为空");
            }
        } else {
            alertMessage("网络错误");
        }

    }
}
